From: Jonathan Dieter Date: Mon, 26 Mar 2018 09:29:50 +0000 (+0300) Subject: Fix compression so it works correctly if there's no dict X-Git-Tag: archive/raspbian/1.1.9+ds1-1+rpi1~1^2~339 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=d0466ab2636308232efcaa25dd62be2d046d49b4;p=zchunk.git Fix compression so it works correctly if there's no dict Signed-off-by: Jonathan Dieter --- diff --git a/src/lib/comp/comp.c b/src/lib/comp/comp.c index 59e1bd5..a1a465f 100644 --- a/src/lib/comp/comp.c +++ b/src/lib/comp/comp.c @@ -68,28 +68,32 @@ int zck_comp_init(zckCtx *zck) { if(!zck->comp.init(&(zck->comp))) return False; - if(zck->comp.dict && zck->temp_fd) { - if(!zck->comp.compress(comp, zck->comp.dict, zck->comp.dict_size, &dst, - &dst_size, 0)) - return False; - if(!zck_write(zck->temp_fd, dst, dst_size)) { + if(zck->temp_fd) { + if(zck->comp.dict) { + if(!zck->comp.compress(comp, zck->comp.dict, zck->comp.dict_size, &dst, + &dst_size, 0)) + return False; + if(!zck_write(zck->temp_fd, dst, dst_size)) { + free(dst); + return False; + } + zck_index_add_to_chunk(zck, dst, dst_size, zck->comp.dict_size); free(dst); - return False; - } - zck_index_add_to_chunk(zck, dst, dst_size, zck->comp.dict_size); - free(dst); - dst = NULL; - dst_size = 0; - - if(!zck->comp.end_chunk(comp, &dst, &dst_size, 0)) - return False; - if(!zck_write(zck->temp_fd, dst, dst_size)) { + dst = NULL; + dst_size = 0; + + if(!zck->comp.end_chunk(comp, &dst, &dst_size, 0)) + return False; + if(!zck_write(zck->temp_fd, dst, dst_size)) { + free(dst); + return False; + } + zck_index_add_to_chunk(zck, dst, dst_size, 0); + zck_index_finish_chunk(zck); free(dst); - return False; + } else { + zck_index_finish_chunk(zck); } - zck_index_add_to_chunk(zck, dst, dst_size, 0); - zck_index_finish_chunk(zck); - free(dst); } zck->comp.dict = NULL; zck->comp.dict_size = 0; @@ -133,11 +137,8 @@ int zck_end_chunk(zckCtx *zck) { } /* No point in compressing empty data */ - if(zck->comp.data_size == 0) { - if(!zck_index_finish_chunk(zck)) - return False; + if(zck->comp.data_size == 0) return True; - } char *dst = NULL; size_t dst_size = 0; diff --git a/src/lib/index/index_create.c b/src/lib/index/index_create.c index d0eeeae..17b3fca 100644 --- a/src/lib/index/index_create.c +++ b/src/lib/index/index_create.c @@ -221,15 +221,24 @@ int zck_index_finish_chunk(zckCtx *zck) { if(zck->work_index_item == NULL && !zck_index_create_chunk(zck)) return False; - /* Finalize chunk checksum */ - char *digest = zck_hash_finalize(&(zck->work_index_hash)); - if(digest == NULL) { - zck_log(ZCK_LOG_ERROR, - "Unable to calculate %s checksum for new chunk\n", - zck_hash_name_from_type(zck->index.hash_type)); - return False; + char *digest = NULL; + if(zck->work_index_item->length > 0) { + /* Finalize chunk checksum */ + digest = zck_hash_finalize(&(zck->work_index_hash)); + if(digest == NULL) { + zck_log(ZCK_LOG_ERROR, + "Unable to calculate %s checksum for new chunk\n", + zck_hash_name_from_type(zck->index.hash_type)); + return False; + } + } else { + digest = zmalloc(zck->chunk_hash_type.digest_size); + if(digest == NULL) { + zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n", + zck->chunk_hash_type.digest_size); + return False; + } } - if(!finish_chunk(&(zck->index), zck->work_index_item, digest, True)) return False;